/**
 * Created by spatra on 14-5-7.
 */

function buildDivWithClassName(className){
    return $('<div class=" ' + className + '"></div>');
}
function createMethodDiv(methodName, proPhyPage){
    var $methodDiv = buildDivWithClassName('methodDiv');
    var $title = $('<h2>' + methodName +'</h2>');
    var $table = buildDivWithClassName('tables');
    var $column = buildDivWithClassName('column');
    var $cell = buildDivWithClassName('cell');
    var $interrupt = $('<div class="interrupt vHidden">X</div>');
    var $statistics = $('<div class="statis hidden">缺页率为:<span></span>%</div>');

    for( var i = 0; i < proPhyPage; ++i ){
        $cell.append('<div>-</div>');
    }

    $column.append($cell);
    $column.append($interrupt);
    $table.append($column);
    $methodDiv.append($title);
    $methodDiv.append($table);
    $methodDiv.append($statistics);

    return $methodDiv;
}

function addColumn($methodDiv, addPageNum, rmPageNum){
    var $lastColumn = $methodDiv.find('.tables>div:last-child');
    var $addColumn = $lastColumn.clone();
    var $theTable = $methodDiv.find('.tables');

    if( arguments.length == 1 ){
        $theTable.append( $addColumn );
        $addColumn.find('.interrupt').addClass('vHidden');
        return $addColumn;
    }

    console.log('RM::::' + rmPageNum);
    if( rmPageNum === null  )
        $addColumn.find('.cell>div:last-child').remove();
    else{
        var removed = false;
        $addColumn.find('.cell>div').each( function() {
            if( removed )
                return;

            var $currDiv = $(this);

            if( rmPageNum == parseInt( $currDiv.text() ) ) {
                console.log('RemoDiv:' + parseInt( $currDiv.text() ) );
                $currDiv.remove();
                removed = true;
            }
        });
    }

    $addColumn.find('.cell').prepend('<div>' + addPageNum + '</div>');
    $methodDiv.find('.tables').append($addColumn);

    $addColumn.find('.interrupt').removeClass('vHidden');

    return $addColumn;
}

function getSchedulingQueue(inputString){
    if( inputString == null )
        return null;

    var tmp = inputString.split(' ');
    var theQueue = new Array();
    for( var i = 0; i < tmp.length; ++i ){
        theQueue.push( parseInt( tmp[i] ) );
        if( isNaN( theQueue[i]) ){
            alert("载入的页面调度序列格式有误，请输入整数！");
            return null;
        }
    }

    return theQueue;
}


var schedulingInterval = 1000;

var currIndex = 0;
var scheduleQueue = new Array();
var methods = new Array();
function simulation(){
    if( currIndex == scheduleQueue.length ){
        showStatis();
        alert('模拟已结束');
        $('#startBtn').attr('disabled', false);
        return;
    }

    for(var i = 0; i < methods.length; ++i ){
        methods[i].schedule( scheduleQueue[currIndex]);
    }

    ++currIndex;
    setTimeout(simulation, schedulingInterval);
}

function resetSimulation(theScheduleQueue, theMethods){
    currIndex = 0;
    scheduleQueue = theScheduleQueue;
    methods = theMethods;
}


function removeInArray(theArray, theTarget){
    var targetIndex = null;
    for(var i = 0; i < theArray.length; ++i )
        if( theArray[i] == theTarget ) {
            targetIndex = i;
            break;
        }

    console.log( theArray[targetIndex] );
    if( targetIndex!= null )
        return theArray.splice(targetIndex, 1);
    else
        return null;
}

function showStatis(){
    var total = scheduleQueue.length * 1.0;
    $('.statis').each( function(index,element){
        $(this).find('span').text( methods[index].missingCounter / total * 100 );
    }).slideDown();
}